<!--
 * @Description: 评价标签
 * @Author: xiao li
 * @Date: 2021-07-04 13:00:37
 * @LastEditTime: 2022-01-24 18:24:28
 * @LastEditors: xiao li
-->

<template>
  <div class="lb-shop-refund">
    <top-nav />
    <div class="page-main">
      <el-row class="page-top-operate">
        <el-button
          @click="toChange(0)"
          :type="searchForm.eva_type === 0 ? 'primary' : ''"
          plain
          size="medium"
          >全部</el-button
        >
        <el-button
          @click="toChange(1)"
          :type="searchForm.eva_type === 1 ? 'primary' : ''"
          plain
          size="medium"
          >好评（{{ count.goods || 0 }}）</el-button
        >
        <el-button
          @click="toChange(2)"
          :type="searchForm.eva_type === 2 ? 'primary' : ''"
          plain
          size="medium"
          >中/差评（{{ count.bad || 0 }}）</el-button
        >
      </el-row>
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="所属场地" prop="place_id">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.place_id"
              filterable
              placeholder="请选择所属场地"
            >
              <el-option
                v-for="item in base_place"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
          fixed
        ></el-table-column>
        <el-table-column prop="user_id" label="用户ID"></el-table-column>
        <el-table-column prop="nickName" label="用户昵称"></el-table-column>
        <el-table-column prop="place_title" label="评价场地"></el-table-column>
        <el-table-column prop="order_code" label="评价关联单号" min-width="160">
          <template slot-scope="scope">
            <div
              class="c-link"
              style="cursor: pointer"
              @click="
                $router.push(
                  `${appPath}/banquet/order/detail?id=${scope.row.order_id}`
                )
              "
            >
              {{ scope.row.order_code }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="place_title" label="总体评价">
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.eva_type === 1
                  ? 'primary'
                  : scope.row.eva_type === 2
                  ? 'success'
                  : 'danger'
              "
              >{{ typeType[scope.row.eva_type] }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="star" label="酒店服务评价" min-width="140">
          <template slot-scope="scope">
            <div class="flex-warp">
              <i
                class="iconfont iconyishoucang mr-sm"
                :style="{
                  color: index < scope.row.hotel_star ? '#f12c20' : '#ddd'
                }"
                v-for="(item, index) in 5"
                :key="index"
              ></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="star" label="销售评价" min-width="140">
          <template slot-scope="scope">
            <div class="flex-warp">
              <i
                class="iconfont iconyishoucang mr-sm"
                :style="{
                  color: index < scope.row.sale_star ? '#f12c20' : '#ddd'
                }"
                v-for="(item, index) in 5"
                :key="index"
              ></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="star" label="场地环境" min-width="140">
          <template slot-scope="scope">
            <div class="flex-warp">
              <i
                class="iconfont iconyishoucang mr-sm"
                :style="{
                  color: index < scope.row.place_star ? '#f12c20' : '#ddd'
                }"
                v-for="(item, index) in 5"
                :key="index"
              ></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="text" label="评价内容" min-width="100">
          <template slot-scope="scope"
            ><div
              class="ellipsis"
              style="height: 40px; line-height: 20px"
              v-html="scope.row.text"
            ></div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" min-width="120" label="评价时间">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time | handleTime(1) }}</div>
            <div>{{ scope.row.create_time | handleTime(2) }}</div>
          </template>
        </el-table-column>
        <el-table-column min-width="160" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="toShowDialog(scope.row.id)"
                >{{ $t('action.view') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>

      <el-dialog
        title="评价详情"
        :visible.sync="showDialog"
        width="800px"
        center
      >
        <div style="height: 60vh; overflow: auto" v-if="showDialog">
          <el-form
            @submit.native.prevent
            :model="dialogForm"
            label-width="130px"
            size="mini"
          >
            <el-form-item label="用户ID：">
              <div>{{ dialogForm.user_id }}</div>
            </el-form-item>
            <el-form-item label="用户昵称：">
              <div>{{ dialogForm.user_info.nickName }}</div>
            </el-form-item>
            <el-form-item label="用户头像：">
              <div class="flex-warp">
                <lb-cover
                  :fileList="[{ url: dialogForm.user_info.avatarUrl }]"
                  :isToDel="false"
                  fileType="image"
                  type="more"
                  size="small"
                  :fileSize="1"
                ></lb-cover>
              </div>
            </el-form-item>
            <el-form-item label="总体评价："
              ><el-tag
                :type="
                  dialogForm.eva_type === 1
                    ? 'primary'
                    : dialogForm.eva_type === 2
                    ? 'success'
                    : 'danger'
                "
                >{{ typeType[dialogForm.eva_type] }}</el-tag
              ></el-form-item
            >
            <el-form-item label="酒店服务评价：">
              <div class="flex-warp mt-sm">
                <i
                  class="iconfont iconyishoucang mr-sm"
                  :style="{
                    color: index < dialogForm.hotel_star ? '#f12c20' : '#ddd'
                  }"
                  v-for="(item, index) in 5"
                  :key="index"
                ></i>
              </div>
            </el-form-item>
            <el-form-item label="销售评价：">
              <div class="flex-warp mt-sm">
                <i
                  class="iconfont iconyishoucang mr-sm"
                  :style="{
                    color: index < dialogForm.sale_star ? '#f12c20' : '#ddd'
                  }"
                  v-for="(item, index) in 5"
                  :key="index"
                ></i>
              </div>
            </el-form-item>
            <el-form-item label="场地环境：">
              <div class="flex-warp mt-sm">
                <i
                  class="iconfont iconyishoucang mr-sm"
                  :style="{
                    color: index < dialogForm.place_star ? '#f12c20' : '#ddd'
                  }"
                  v-for="(item, index) in 5"
                  :key="index"
                ></i>
              </div>
            </el-form-item>
            <el-form-item label="评价内容：">
              <div v-html="dialogForm.text"></div>
              <div class="flex-warp">
                <lb-cover
                  :fileList="dialogForm.imgs"
                  :isToDel="false"
                  size="small"
                  fileType="image"
                  type="more"
                  :fileSize="dialogForm.imgs.length"
                ></lb-cover>
              </div>
            </el-form-item>
            <el-form-item label="评价时间：">
              <div>
                {{ dialogForm.create_time | handleTime() }}
              </div>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">{{
            $t('action.cancel')
          }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      appPath: '',
      loading: false,
      base_place: [],
      typeType: {
        1: '好评',
        2: '中评',
        3: '差评'
      },
      count: { goods: 0, bad: 0 },
      searchForm: {
        page: 1,
        limit: 10,
        eva_type: 0,
        place_id: ''
      },
      tableData: [],
      total: 0,
      showDialog: false,
      dialogForm: {}
    }
  },
  async activated () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
    await this.getBaseInfo()
    this.getTableDataList()
  },
  methods: {
    async getBaseInfo () {
      let { data } = await this.$api.banquet.placeSelect()
      this.base_place = data
    },
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    toChange (type) {
      this.searchForm.eva_type = type
      this.getTableDataList(1)
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      if (searchForm.eva_type === 1) {
        searchForm.good = 1
      }
      if (searchForm.eva_type === 2) {
        searchForm.bad = 1
      }
      delete searchForm.eva_type
      let { code, data } = await this.$api.banquet.placeEvaluateList(searchForm)
      this.loading = false
      if (code !== 200) return
      data.data.map(item => {
        let text = item.text || '没有填写评价'
        item.text = text.replace(/\n/g, '<br>')
      })
      this.count = data.count
      this.tableData = data.data
      this.total = data.total
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      })
        .then(() => {
          this.updateItem(id, -1)
        })
        .catch(() => { })
    },
    async updateItem (id, status) {
      this.$api.banquet.evaluateUpdate({ id, status }).then((res) => {
        if (res.code === 200) {
          this.$message.success(
            this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper')
          )
          if (status === -1) {
            this.searchForm.page =
              this.searchForm.page <
                Math.ceil((this.total - 1) / this.searchForm.limit)
                ? this.searchForm.page
                : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    async toShowDialog (id) {
      let { code, data } = await this.$api.banquet.evaluateInfo({ id })
      if (code !== 200) return
      data.imgs = data.imgs && data.imgs.length > 0 ? data.imgs.map(bitem => {
        return { url: bitem }
      }) : []
      let text = data.text || '没有填写评价'
      data.text = text.replace(/\n/g, '<br>')
      this.dialogForm = data
      this.showDialog = !this.showDialog
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-shop-refund {
  .page-main {
    width: 100%;

    .el-select,
    .el-input-number,
    .el-input {
      width: 200px;
    }

    .iconyishoucang {
      font-size: 18px;
    }
  }
}

.none {
  display: none;
}
</style>
<style media="print">
.none {
  display: block;
}
</style>
